package com.lantosec.hmrp.modules.workflow.listener;

import com.lantosec.hmrp.modules.sysdata.service.ISysDataProcessService;
import com.lantosec.hmrp.modules.sysdata.service.ISysDataProcessTaskService;
import com.lantosec.hmrp.modules.system.service.ISysUserService;
import com.lantosec.hmrp.modules.system.service.ISysWorkdayService;
import com.lantosec.hmrp.modules.workflow.enumeration.ProcessVariableEnum;
import com.lantosec.hmrp.modules.workflow.service.IWorkflowRepositoryService;
import com.lantosec.hmrp.modules.workflow.service.IWorkflowRuntimeService;
import com.lantosec.hmrp.modules.workflow.vo.CustomAttributeVO;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.api.Task;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Objects;

/**
 * 流程任务创建会签监听类
 *
 * @author lichaowei
 * @date 2020/5/26
 */
@Component
@Log4j2
public class SetMultiGroupsListener extends UserTaskCreateListener implements TaskListener {
    private static final long serialVersionUID = 1L;

    @Autowired
    ISysUserService sysUserService;
    @Autowired
    IWorkflowRepositoryService workflowService;
    @Autowired
    ISysWorkdayService workdayService;
    @Autowired
    IWorkflowRuntimeService workflowRuntimeService;
    @Autowired
    private IWorkflowRepositoryService workflowRepositoryService;
    @Autowired
    private ISysDataProcessTaskService dataProcessTaskService;
    @Autowired
    private ISysDataProcessService dataProcessService;

    @Override
    public void notify(DelegateTask delegateTask) {
        log.info("流程任务创建会签监听事件----->taskId:" + delegateTask.getId());

        String processDefinitionId = delegateTask.getProcessDefinitionId();
        String taskDefinitionKey = delegateTask.getTaskDefinitionKey();

        String userId = (String) delegateTask.getVariable(ProcessVariableEnum.USER_ID.getCode());
        String businessKey = (String) delegateTask.getVariable(ProcessVariableEnum.BUSINESS_KEY.getCode());
        // 获取节点自定义属性对象
        CustomAttributeVO customAttributeVO = workflowRepositoryService.getCustomAttribute(taskDefinitionKey, processDefinitionId);
        String operator = customAttributeVO.getOperator();
        // 获取节点的操作人列表
        List<String> optUserList = super.getTaskOptUserList(delegateTask, operator, userId);

        // 同一个任务节点只要执行一次，排除会签节点多次执行的问题
        // 会签数量Key
        String MULTIGROUPSCOUNT = "multiGroupsCount";
        Integer count = (Integer) delegateTask.getVariable(MULTIGROUPSCOUNT);
        if (Objects.isNull(count) || count == 0) {
            count = 1;
            // 设置节点的提醒
            super.setTaskNotice(delegateTask, customAttributeVO.getNotice(), userId, optUserList);
            // 抄送/只会，只能查看不能编辑
            workflowRuntimeService.setTaskRelay((Task) delegateTask, customAttributeVO.getRelay(), userId, null, Boolean.TRUE);
            // 催办设置
            super.setTaskHasten(delegateTask, customAttributeVO.getHasten(), userId, optUserList);

            // 会签相关 将通过人数，未通过人数，重新置为0，退回的时候才能重新计算
            delegateTask.setVariable(ProcessVariableEnum.AGREE_COUNT.getCode(), 0);
            delegateTask.setVariable(ProcessVariableEnum.DISAGREE_COUNT.getCode(), 0);

            // 保存流程相关信息
            dataProcessService.updateProcess(delegateTask.getProcessInstanceId(), delegateTask.getId(), delegateTask.getTaskDefinitionKey()
                    , delegateTask.getName(), null, null, businessKey);
        } else {
            count++;
        }
        String currentUserId = CollectionUtils.isNotEmpty(optUserList) ? optUserList.get(count - 1) : "";
        delegateTask.addCandidateUser(currentUserId);
        delegateTask.setVariable(MULTIGROUPSCOUNT, count);

        // 保存流程节点操作信息
        dataProcessTaskService.insertTask((Task) delegateTask, currentUserId, customAttributeVO.getFormLayoutId());

        // 自动流转，如果任务的下一个节点处理人是当前处理人，则自动完成当前任务
        super.taskAutoComplete(delegateTask, customAttributeVO.getIsAutoComplete(), optUserList, userId, customAttributeVO.getFormLayoutId());
    }

}
